package data.features;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

import data.dynamodbfunctions.Persistence_Helper_Feature;
import data.dynamodbfunctions.Persistence_Helper_FeatureAssociation;
import data.exceptions.Dynamo_DB_Functions_Exception;
import data.meta.MetaAnnotations.MetaMethod;
import data.meta.MetaAnnotations.MetaParam;
import data.meta.Metadata;
import data.objects.ClassSpy;
import data.objects.Select_Condition;
/* @author Robin
*
*/
@DynamoDBTable(tableName="CC_Feature")
public class Feature {
	
	public String TenantID;
	public String ID;
	public String name;
	public String value;
	public String type;
	public String description;
	
	public Feature() {
		this.ID = ""+UUID.randomUUID();
	}

	public Feature(String TenantID, String name,
			String value, String type, String description) {
		super();
		this.TenantID = TenantID;
		this.ID =""+UUID.randomUUID();
		this.name = name;
		this.value = value;
		this.type=type;
		this.description = description;
	}

	@DynamoDBHashKey(attributeName="ID")
	public String getTenantID() {return TenantID;}
	public void setTenantID(String TenantID) {this.TenantID = TenantID;}

	@DynamoDBRangeKey(attributeName="Range_ID")
	public String getFeatureID() {return ID;}
	public void setFeatureID(String ID) {this.ID = ID;}

	@DynamoDBAttribute(attributeName="Name")
	public String getName() {return name;}
	public void setName(String name) {this.name = name;}
	
	@DynamoDBAttribute(attributeName="Type")
	public String getType() {return type;}
	public void setType(String type) {this.type = type;}

	@DynamoDBAttribute(attributeName="Value")
	public String getValue() {return value;}
	public void setValue(String value) {this.value = value;}

	@DynamoDBAttribute(attributeName="Description")
	public String getDescription() { return description;}
	public void setDescription(String description) {this.description = description;}
	
	@DynamoDBIgnore
	public List<Feature> toSubFeatures(){
		List<Feature> subFeatures = new ArrayList<Feature>();
		
		List<Select_Condition> conditionlist=new ArrayList<Select_Condition>();
		conditionlist.add(new Select_Condition("SourceFeatureID","=",this.ID));
		
		try{
			List<FeatureAssociation> associationList = Persistence_Helper_FeatureAssociation.getInstance().readwithAttributes(this.TenantID,conditionlist);
			for(FeatureAssociation a: associationList){
				subFeatures.add(Persistence_Helper_Feature.getInstance().read(this.TenantID, a.getTargetFeatureID()));			
			}
		} catch (Dynamo_DB_Functions_Exception e) {
			
		}		
		return subFeatures;
	}
	
	
	@DynamoDBIgnore
	public Feature toParentFeature() {
		Feature parentFeature = null;
		
		List<Select_Condition> conditionlist=new ArrayList<Select_Condition>();
		conditionlist.add(new Select_Condition("TargetFeatureID","=",this.ID));
		try {
			List<FeatureAssociation> associationList = Persistence_Helper_FeatureAssociation.getInstance().readwithAttributes(this.TenantID,conditionlist);
			parentFeature = Persistence_Helper_Feature.getInstance().read(this.TenantID, associationList.get(0).getSourceFeatureID());
		} catch (Dynamo_DB_Functions_Exception e) {
			parentFeature =null;
		}		
		return parentFeature;
	}
	
//	@MetaMethod
	@DynamoDBIgnore
	public static Feature create(@MetaParam("TenantID") String TenantID, @MetaParam("name") String name, @MetaParam("value") String value, @MetaParam("type") String type, @MetaParam("description") String description) throws Dynamo_DB_Functions_Exception {
		Feature createFeature = new Feature(TenantID, name, value, type, description);
		return Persistence_Helper_Feature.getInstance().create(createFeature);	
	}
	
	@MetaMethod
	@DynamoDBIgnore
	public static Feature read(@MetaParam("TenantID") String tenant_id, @MetaParam("id") String id) throws Dynamo_DB_Functions_Exception {
		Feature readobject=Persistence_Helper_Feature.getInstance().read(tenant_id,id);
		return readobject;
	}
	
	@MetaMethod
	@DynamoDBIgnore
	public static List<Feature> readAll(@MetaParam("TenantID") String tenant_id) throws Dynamo_DB_Functions_Exception {
		List<Feature> readobject=Persistence_Helper_Feature.getInstance().readAllwithHashKey(tenant_id);
		return readobject;
	}
	
	@MetaMethod
	@DynamoDBIgnore
	public static Feature update(@MetaParam("TenantID") String tenant_id, @MetaParam("ID") String ID, @MetaParam("name") String name, @MetaParam("value") String value, @MetaParam("type") String type, @MetaParam("description") String description) throws Dynamo_DB_Functions_Exception {
		Feature updateFeature =Persistence_Helper_Feature.getInstance().read(tenant_id,ID);
		if(!"".equals(name)){
			updateFeature.setName(name);
		}
		if(!"".equals(value)){
			updateFeature.setValue(value);
		}
		if(!"".equals(type)){
			updateFeature.setType(type);
		}
		if(!"".equals(description)){
			updateFeature.setDescription(description);
		}
		return Persistence_Helper_Feature.getInstance().update(updateFeature);
	}
	
//	@MetaMethod
	@DynamoDBIgnore
	public static Feature delete(@MetaParam("TenantID") String tenant_id, @MetaParam("id") String id) throws Dynamo_DB_Functions_Exception {
		return Persistence_Helper_Feature.getInstance().delete(tenant_id,id);
	} 
	
	@DynamoDBIgnore
	public static Metadata getMetadata(){
		return ClassSpy.getMetadata("data.features.Feature");
	}
}


